#include <os/semaphore.h>
#include "include/FreeRTOS.h"
#include "include/semphr.h"


/**
 * @brief Initialize a semaphore
 * @param sema semaphore handle
 * @param val initial value
 * @return none
 */
void semaphore_init(struct semaphore *sema, int val)
{
    sema->semaphore_ptr = xSemaphoreCreateCounting(val, val);
}


/**
 * @brief Down a semaphore
 * @param sema semaphore handle
 * @return none
 */
void semaphore_down(struct semaphore *sema)
{
    xSemaphoreTake(sema->semaphore_ptr, portMAX_DELAY);
}


/**
 * @brief Up a semaphore
 * @param sema semaphore handle
 * @return none
 */
void semaphore_up(struct semaphore *sema)
{
    xSemaphoreGive(sema->semaphore_ptr);
}


/**
 * @brief Destroy a semaphore
 * @param sema semaphore handle
 * @return none
 */
void semaphore_destroy(struct semaphore *sema)
{
    vSemaphoreDelete(sema->semaphore_ptr);
}
